import { request, downloadBlobFile } from 'src/utils';

export interface WorkOrderDto {
  orderId: IdType; // 工单ID
  orderNo: string; // 工单编号
  orderType: number; // 工单类型
  description: string; // 工单描述
  orderStatus: number; // 工单状态
  submitPerson: string; // 提交人
  submitTel: string; // 提交人电话
  submitAreaId: string; // 小区ID
  submitRoomId: string; // 提交人房号
  submitAddr: string; // 提交人地址
  submitTime: Date; // 提交时间
  rateStars: number; // 评价星级
  rateContent: string; // 评价内容
  handleUser: number; // 工单处理人账号
  handleUserName: string; // 工单处理人姓名
  handleResult: string; // 处理结果
  createdBy: string; // 创建人
  createdTime: Date; // 创建时间
}

const baseUrl = '/api/workorder/workOrder';

/**
 * 查单个
 */
export function readData(orderId: IdType): Promise<WorkOrderDto> {
  return request.get(baseUrl + '/' + orderId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<WorkOrderDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(workOrder: WorkOrderDto): Promise<WorkOrderDto> {
  return workOrder.orderId ? request.put(baseUrl + '/' + workOrder.orderId, workOrder) : request.post(baseUrl, workOrder);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<WorkOrderDto>;
